package br.com.tcc.view;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class Salvar {

	private JFrame frame;
	private JRadioButton radioPlanoExecucao; 
	private JRadioButton radioRecomendacao;
	
	private static void criaTela() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			Salvar window = new Salvar();
			window.frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void inicializarTela(){
		criaTela();
	}

	public Salvar() {
		inicializarTelaSalvar();
	}

	private void inicializarTelaSalvar() {
		frame = new JFrame();
		frame.setBounds(100, 100, 350, 250);
		frame.setTitle("Salvar");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setMinimumSize(new Dimension(350,250));
		frame.setLocationRelativeTo(Main.frmOtimizadorSql);
		
		JLabel lblOQueDeseja = new JLabel("O que deseja salvar?");
		lblOQueDeseja.setFont(new Font("Tahoma", Font.PLAIN, 17));
		
		radioPlanoExecucao = new JRadioButton("Plano de execu\u00E7\u00E3o");
		
		radioRecomendacao = new JRadioButton("Recomenda\u00E7\u00F5es");
		
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(radioPlanoExecucao);
		buttonGroup.add(radioRecomendacao);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon(Salvar.class.getResource("/img/disk.png")));
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				salvarOpcaoSelecionada();
			}
		});
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setIcon(new ImageIcon(Salvar.class.getResource("/img/cancel.png")));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(44)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(radioRecomendacao)
								.addComponent(radioPlanoExecucao)
								.addComponent(lblOQueDeseja)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(125)
							.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnCancelar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
					.addGap(16))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(28)
					.addComponent(lblOQueDeseja)
					.addGap(30)
					.addComponent(radioPlanoExecucao)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(radioRecomendacao)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCancelar, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(43, Short.MAX_VALUE))
		);
		frame.getContentPane().setLayout(groupLayout);
		radioPlanoExecucao.setSelected(true);
	}
	
	public void salvarOpcaoSelecionada(){
		if (radioPlanoExecucao.isSelected()) {
			if(Arquivo.salvarArquivo(Main.planoDeExecucao, Main.frmOtimizadorSql, "plano")){
				frame.setVisible(false);
			}
		}else if (radioRecomendacao.isSelected()) {
			if(Arquivo.salvarArquivo(Main.recomendacoesCompleto, Main.frmOtimizadorSql, "recomendacao")){
				frame.setVisible(false);
			}
		}
		
	}
}
